﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Product : System.Web.UI.Page
{
    BonSai bs = new BonSai();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request["categoryID"] == null)
            {
                InitializeProduct(1);
            }
            else
            {
                try
                {
                    int categoryID = Int32.Parse(Request["categoryID"]);
                    listCatalogProduct.SelectedIndex = categoryID-1;
                    InitializeProduct(categoryID);
                }
                catch (Exception)
                {
                    Response.Redirect("Default.aspx");
                }
            }

            BonSai bs = new BonSai();
            BonSaiTableAdapters.CategoriesTableAdapter bsADT = new BonSaiTableAdapters.CategoriesTableAdapter();
            bsADT.Fill(bs.Categories);
            listCatalogProduct.DataSource = bs.Categories;
            listCatalogProduct.DataTextField = "CategoryName";
            listCatalogProduct.DataValueField = "CategoryID";
            listCatalogProduct.DataBind();            
        }
             
    }
    protected void listCatalogProduct_SelectedIndexChanged(object sender, EventArgs e)
    {
        InitializeProduct(Convert.ToInt32(listCatalogProduct.SelectedValue));    
    }
    
    void InitializeProduct(int idCategory)
    {
        BonSaiTableAdapters.ProductsTableAdapter pdADT = new BonSaiTableAdapters.ProductsTableAdapter();
        BonSai.ProductsDataTable pd = pdADT.GetProductByCategoryID(idCategory);        
        listProduct.DataSource = pd;
        listProduct.DataBind();
    }
}
